## VRRP Instances definitions
vrrp_script chk_{{ instance.name | lower }} {
    script              "{{ instance.check_script }}"
    interval            3        # check every 3 seconds
    weight              3        # add 3 points of priority if OK
}

vrrp_instance {{ instance.name | upper }} {
    state               {{ (inventory_hostname == instance.master) | ternary('MASTER', 'BACKUP') }}
    interface           {{ instance.interface }}
    virtual_router_id   {{ instance.virtual_ipaddress | split('.') | last }}
    priority            {{ (inventory_hostname == instance.master) | ternary('101', '99') }}
    advert_int          {{ instance.advert_int | default('3') }}
    unicast_src_ip      {{ hostvars[inventory_hostname]['ansible_' + instance.interface]['ipv4']['address'] }}

    unicast_peer {
{% for peer in groups['nginx'] %}
        {{ hostvars[peer]['ansible_' + instance.interface]['ipv4']['address'] }}
{% endfor %}
    }

    authentication {
        auth_type       PASS
        auth_pass       {{ lookup('password', inventory_dir + '/credentials/passwords/passwd_keepalived_' + instance.name, length=8) }}
    }

    virtual_ipaddress {
        {{ instance.virtual_ipaddress  }}/24
    }

    track_script {
        chk_{{ instance.name | lower }}
    }
}
